<?php
/**
 * YWebApplication.php
 *
 * @author Hua Yang <htmlcook@gmail.com>
 * @since 2.0
 */

/**
 * Class YWebApplication
 */
class YWebApplication extends CWebApplication
{
	/**
	 * 应用程序版本
	 *
	 * @var string
	 */
	public $version = '1.0.0';

	/**
	 * 后台控制器映射
	 *
	 * @var array
	 */
	public $backendControllerMap = array();

	/**
	 * @inheritdoc
	 */
	protected function registerCoreComponents()
	{
		parent::registerCoreComponents();

		$components = array(
			'lock' => array(
				'class' => 'common.components.lock.YDatabaseLockBackend'
			),
			'mail' => array(
				'class' => 'common.components.mail.swiftmailer.Mailer',
			),
			'image' => array(
				'class' => 'common.components.image.YImage',
			),
			'option' => array(
				'class' => 'common.components.option.YOption',
			),
			'watchdog' => array(
				'class' => 'YWatchdog',
			),
		);

		$this->setComponents($components);
	}

	/**
	 * @return YLockBackendInterface
	 */
	public function getLock()
	{
		return $this->getComponent('lock');
	}

	/**
	 * @return YMailerInterface
	 */
	public function getMail()
	{
		return $this->getComponent('mail');
	}

	/**
	 * @return YImageInterface
	 */
	public function getImage()
	{
		return $this->getComponent('image');
	}

	/**
	 * @return Setting
	 */
	public function getOption()
	{
		return $this->getComponent('option');
	}

	public function getWatchdog()
	{
		return $this->getComponent('watchdog');
	}

	public function createController($route,$owner=null)
	{
		if($owner===null)
			$owner=$this;
		if(($route=trim($route,'/'))==='')
			$route=$owner->defaultController;
		$caseSensitive=$this->getUrlManager()->caseSensitive;

		$route.='/';
		while(($pos=strpos($route,'/'))!==false)
		{
			$id=substr($route,0,$pos);
			if(!preg_match('/^\w+$/',$id))
				return null;
			if(!$caseSensitive)
				$id=strtolower($id);
			$route=(string)substr($route,$pos+1);
			if(!isset($basePath))  // first segment
			{
				if(isset($owner->controllerMap[$id]))
				{
					return array(
						Yii::createComponent($owner->controllerMap[$id],$id,$owner===$this?null:$owner),
						$this->parseActionParams($route),
					);
				}

				if (isset($owner->backendControllerMap) && isset($owner->backendControllerMap[$id]))
				{
					$controllerPath = dirname(Yii::getPathOfAlias($owner->backendControllerMap[$id]['class']));
					$this->setViewPath($controllerPath . '/../views');
					return array(Yii::createComponent($owner->backendControllerMap[$id],$id,$owner===$this?null:$owner),
						$this->parseActionParams($route),
					);
				}

				if(($module=$owner->getModule($id))!==null)
					return $this->createController($route,$module);

				$basePath=$owner->getControllerPath();
				$controllerID='';
			}
			else
				$controllerID.='/';
			$className=ucfirst($id).'Controller';
			$classFile=$basePath.DIRECTORY_SEPARATOR.$className.'.php';

			if($owner->controllerNamespace!==null)
				$className=$owner->controllerNamespace.'\\'.$className;

			if(is_file($classFile))
			{
				if(!class_exists($className,false))
					require($classFile);
				if(class_exists($className,false) && is_subclass_of($className,'CController'))
				{
					$id[0]=strtolower($id[0]);
					return array(
						new $className($controllerID.$id,$owner===$this?null:$owner),
						$this->parseActionParams($route),
					);
				}
				return null;
			}
			$controllerID.=$id;
			$basePath.=DIRECTORY_SEPARATOR.$id;
		}
	}

	/**
	 * 清理资源
	 */
	public function clearAssets()
	{
		ignore_user_abort(true);
		set_time_limit(0);
		$assetsPath = $this->getAssetManager()->getBasePath();
		CFileHelper::removeDirectory($assetsPath);
		@mkdir($assetsPath);
	}
}